﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="TestAssignDouble.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential.Tests.CMDenseMatrixTest
{
    #region Usings

    using System;

    using NUnit.Framework;

    #endregion

    /// <summary>
    ///     Test suite for <see cref="CMDenseMatrixExtensions.Assign(CMDenseMatrix{double},CMDenseMatrix{double})"/>.
    /// </summary>
    [TestFixture]
    [Category("ExulLibrary.Mathematics.LinearAlgebra.Sequential.CMDenseMatrixExtensions")]
    public sealed class TestAssignDouble
    {
        /// <summary>
        ///     Tests the method when source and target parameters have the same size.
        /// </summary>
        [Test]
        public void WhenSourceAndTargetHaveTheSameSize()
        {
            // Arrange
            var sourceData = new[]
                             {
                                 new double[] { 10, 20 },
                                 new double[] { 30, 40 },
                                 new double[] { 50, 60 }
                             };
            var source = new CMDenseMatrix<double>(sourceData);
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2 },
                    new double[] { 3, 4 },
                    new double[] { 5, 6 }
                });

            // Act
            target.Assign(source);

            // Assert
            Assert.That(target.Values, Is.Not.SameAs(source.Values) & Is.EquivalentTo(sourceData));
        }

        /// <summary>
        ///     Tests the method when source parameter has less count of rows than target parameter.
        /// </summary>
        [Test]
        public void WhenSourceHasLessRowsCountThanTarget()
        {
            // Arrange
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10 },
                    new double[] { 20 },
                    new double[] { 30 }
                });
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2 },
                    new double[] { 3, 4 },
                    new double[] { 5, 6 }
                });
            var expectedResult = new[]
                                 {
                                     new double[] { 10, 2 },
                                     new double[] { 20, 4 },
                                     new double[] { 30, 6 }
                                 };

            // Act
            target.Assign(source);

            // Assert
            Assert.That(target.Values, Is.Not.SameAs(source.Values) & Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when source parameter has greater count of rows than target parameter.
        /// </summary>
        [Test]
        public void WhenSourceHasGreaterRowsCountThanTarget()
        {
            // Arrange
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20, 30 },
                    new double[] { 40, 50, 60 },
                    new double[] { 70, 80, 90 }
                });
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2 },
                    new double[] { 3, 4 },
                    new double[] { 5, 6 }
                });
            var expectedResult = new[]
                                 {
                                     new double[] { 10, 20 },
                                     new double[] { 40, 50 },
                                     new double[] { 70, 80 }
                                 };

            // Act
            target.Assign(source);

            // Assert
            Assert.That(target.Values, Is.Not.SameAs(source.Values) & Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when source parameter has less count of columns than target parameter.
        /// </summary>
        [Test]
        public void WhenSourceHasLessColumnsCountThanTarget()
        {
            // Arrange
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 }
                });
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2 },
                    new double[] { 3, 4 },
                    new double[] { 5, 6 }
                });
            var expectedResult = new[]
                                 {
                                     new double[] { 10, 20 },
                                     new double[] { 30, 40 },
                                     new double[] { 5, 6 }
                                 };

            // Act
            target.Assign(source);

            // Assert
            Assert.That(target.Values, Is.Not.SameAs(source.Values) & Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when source parameter has greater count of columns than target parameter.
        /// </summary>
        [Test]
        public void WhenSourceHasGreaterColumnsCountThanTarget()
        {
            // Arrange
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 },
                    new double[] { 50, 60 },
                    new double[] { 70, 80 }
                });
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2 },
                    new double[] { 3, 4 },
                    new double[] { 5, 6 }
                });
            var expectedResult = new[]
                                 {
                                     new double[] { 10, 20 },
                                     new double[] { 30, 40 },
                                     new double[] { 50, 60 }
                                 };

            // Act
            target.Assign(source);

            // Assert
            Assert.That(target.Values, Is.Not.SameAs(source.Values) & Is.EquivalentTo(expectedResult));
        }

#if SAFE

        /// <summary>
        ///     Tests the method when source parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenSourceIsNull()
        {
            // Arrange
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[2],
                    new double[2],
                    new double[2]
                });

            // Act and Assert
            Assert.That(
                () => target.Assign(null),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("source"));
        }

        /// <summary>
        ///     Tests the method when target parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenTargetIsNull()
        {
            // Arrange
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[2],
                    new double[2],
                    new double[2]
                });

            // Act and Assert
            Assert.That(
                () => CMDenseMatrixExtensions.Assign(null, source),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("target"));
        }

#endif
    }
}